package com.example.xianji.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.R;
import com.example.xianji.adapter.MyZiZhi_Adapter;
import com.example.xianji.bean.ZiZhi;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main_ZiZhiActivity extends Activity {
    private TextView zizhi_name;
    private ImageView zizhi_image;
    private RelativeLayout my_zizhi_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private Button up_load_zizhi;
    private MyZiZhi_Adapter mMyZiZhi_Adapter;
    private ArrayList<ZiZhi> zizhi_list = new ArrayList<ZiZhi>();
    private ListView zizhi_listview;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                zizhi_listview.setAdapter(mMyZiZhi_Adapter);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__zi_zhi);
        up_load_zizhi = (Button) findViewById(R.id.up_load_zizhi);
        my_zizhi_back = (RelativeLayout) findViewById(R.id.my_zizhi_relative_back);
        zizhi_listview = (ListView) findViewById(R.id.zizhi_listview);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        mMyZiZhi_Adapter = new MyZiZhi_Adapter(this, zizhi_list);

        get_zizhi_data();

        up_load_zizhi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Main_ZiZhiActivity.this, Main_Up_Load_ZiZhiActivity.class));
            }
        });

        my_zizhi_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_ZiZhiActivity.this.setResult(Contant.back_resultCode);
                Main_ZiZhiActivity.this.finish();
            }
        });
    }

    /**
     * 请求认证结果
     */
    public void get_zizhi_data() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        httpUtils.send(HttpMethod.POST, Contant.zizhi, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                JSONObject object;
                try {
                    object = new JSONObject(arg0.result);
                    Toast.makeText(Main_ZiZhiActivity.this, object.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (object.getInt("status") == 1) {
                        JSONArray array = object.getJSONArray("data");
                        if (array.length() > 0) {
                            zizhi_list.clear();
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject ob = array.getJSONObject(i);
                                ZiZhi mZiZhi = new ZiZhi();
                                mZiZhi.setName(ob.getString("name"));
                                mZiZhi.setPath(ob.getString("path"));
                                mZiZhi.setTitle(ob.getString("title"));
                                mZiZhi.setUpload_id(ob.getInt("upload_id"));
                                zizhi_list.add(mZiZhi);
                            }
                            handler.sendEmptyMessage(1);
                        } else {
                            Toast.makeText(Main_ZiZhiActivity.this, "您还没有上传资质哦！", Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (Main_Up_Load_ZiZhiActivity.up_load == 1) {
            get_zizhi_data();
            Main_Up_Load_ZiZhiActivity.up_load = 0;
        }
    }
}
